package com.tjufe;

public class Test2 {
    private static String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    public static void main(String[] args) {
       // 目标：生产指定位数的验证码返回，每位只能是数字或大写字母或小写字母
        int length = 6;
        String code = getCode(length);
        System.out.println(code);

    }
    public static String getCode(int n){
        String code = "";
        for (int i = 0; i < n; i++) {
            //当前位置随机生成一个数字或大写字母或小写字母，数字0，大写1，小写2
            int type = (int)(Math.random()*3);
            switch (type){
                case 0:
                    //0-9
                    int num = (int)(Math.random()*10);
                    code += str.charAt(num);
                    break;
                case 1:
                    //A-Z
                    int big = (int)(Math.random()*26);
                    code += str.charAt(big+10);
                    break;
                case 2:
                    //a-z
                    int small = (int)(Math.random()*26);
                    code += str.charAt(small+36);
                    break;
            }

        }
        return code;
    }
}
